/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.stylefeng.guns.modular.grape.controller;

import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.constant.dictmap.DeptDict;
import cn.stylefeng.guns.modular.grape.entity.Enterprise;
import cn.stylefeng.guns.modular.grape.service.EnterpriseService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.trim;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @entor wmao
 */
@Controller
@RequestMapping("/config/ent")
public class EnterpriseController extends BaseController{
    
    @Autowired
    private EnterpriseService enterpriseService;

    private String PREFIX = "/modular/grape/config/ent/";

    /**
     * 跳转到添加商户接口
     *
     * @param model
     * @return 
     * @entor wmao
     * @Date 2018/12/23 4:56 PM
     */
    @RequestMapping("/ent_add")
    public String entAdd(Model model) {
        Enterprise ent = enterpriseService.getOnlyOne();
        if(nonNull(ent)){
            model.addAttribute("entprise", ent);
        }
        return PREFIX + "ent_add.html";
    }

    /**
     * 新增商户信息
     *
     * @entor wmao
     * @param ent
     * @return
     * @Date 2018/12/23 4:57 PM
     */
    @BussinessLog(value = "添加商户接口", key = "simpleName", dict = DeptDict.class)
    @RequestMapping(value = "/saveorupdate")
    @Permission
    @ResponseBody
    public ResponseData addOrUpdate(Enterprise ent) {
        if (nonNull(ent)) {
            if (isNotBlank(trim(ent.getId()))) {
                enterpriseService.editEnt(ent);
            } else {
                enterpriseService.addEnt(ent);
            }
        }
        return SUCCESS_TIP;
    }

}
